package com.ygqh.baby.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.KsdStockMapper;
import com.ygqh.baby.model.YgShoppingCarModel;
import com.ygqh.baby.po.KsdStock;
import com.ygqh.baby.po.KsdStockExample;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.po.YgSku;
import com.ygqh.baby.po.YgStock;
import com.ygqh.baby.service.KsdStockService;

@Service
public class KsdStockServiceImpl implements KsdStockService {

	@Autowired
	private KsdStockMapper ksdStockMapper;
	@Autowired
	private DocumentConstant documentConstant;

	@Override
	public List<KsdStock> find() {
		KsdStockExample example = new KsdStockExample();
		example.createCriteria();
		return ksdStockMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		KsdStockExample example = new KsdStockExample();
		example.createCriteria();
		return ksdStockMapper.countByExample(example);
	}

	@Override
	public KsdStock findById(Long id) {
		return ksdStockMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(KsdStock ksdStock) {
		return ksdStockMapper.insertSelective(ksdStock);
	}

	@Override
	public int update(KsdStock ksdStock) {
		return ksdStockMapper.updateByPrimaryKeySelective(ksdStock);
	}

	@Override
	public int saveOrUpdate(KsdStock ksdStock) {
		int r = 0;
		if (ksdStock.getId() != null) {
			r = this.update(ksdStock);
		} else {
			r = this.save(ksdStock);
		}
		return r;
	}

	@Override
	public List<KsdStock> findStockBySkuCode(List<String> skuCodes, Long supplerId) {
		return ksdStockMapper.selectStockBySkuCode(skuCodes, supplerId);
	}

	@Override
	public Message checkStockNum(List<KsdStock> stockList, List<YgShoppingCarModel> list) {

		for (YgShoppingCarModel car : list) {
			for (KsdStock stock : stockList) {
				if (car.getSkuCode().equals(stock.getSkuCode())) {
					if (car.getQuantity().compareTo(stock.getAmount()) > 0) {
						return Message.error("【" + car.getProductName() + "】" + documentConstant.getSTOCK_LESS(), car.getProductName());
					}
				}
			}
		}
		return Message.success("");
	}

	@Override
	public int updateStockBatch(List<KsdStock> stockList) {

		return ksdStockMapper.updateStockBatch(stockList);
	}
	@Override
	public int updateStockBySkuCode(KsdStock stock) {
		KsdStockExample stockExample=new KsdStockExample();
		stockExample.createCriteria().andSkuCodeEqualTo(stock.getSkuCode());
		return ksdStockMapper.updateByExample(stock, stockExample);
	}

	@Override
	public ResultSet<YgStock> search(QueryInfo queryInfo, String q,
			Long groupId, String productName, String productCode,
			String skuCode, Long amount) {
		List<YgStock> list = ksdStockMapper.selectSuper(queryInfo, q,groupId,productName,productCode,skuCode,amount);
		int count = ksdStockMapper.countSuper(q,groupId,productName,productCode,skuCode,amount);
		
		ResultSet<YgStock> resultSet = new ResultSet<YgStock>(count, list);
		return resultSet;
	}

	@Override
	public void updateStockBySkuList(List<YgStock> list, Long supplierId) {
		ksdStockMapper.updateStockBySkuList(list,supplierId);
		
	}

	@Override
	public List<YgSku> findByProductId(Long productId) {
		return ksdStockMapper.findByProductId(productId);
	}

	
	

}
